﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setCentralWidget(ui->stackedWidget);//设置界面中心组件

    iniData();


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::iniData()
{
    //设置视图自动拉伸
    ui->tvStu->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvCheck->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvStudentWIndow->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    //设置模型列数
    m_studentModel.setColumnCount(5);
    m_checkModel.setColumnCount(5);
    m_courseModel.setColumnCount(3);
    m_teacherModel.setColumnCount(3);

    //设置模型表头
    m_studentModel.setHeaderData(0,Qt::Horizontal,"学号");
    m_studentModel.setHeaderData(1,Qt::Horizontal,"姓名");
    m_studentModel.setHeaderData(2,Qt::Horizontal,"性别");
    m_studentModel.setHeaderData(3,Qt::Horizontal,"年龄");
    m_studentModel.setHeaderData(4,Qt::Horizontal,"班级");

    m_checkModel.setHeaderData(0,Qt::Horizontal,"缺课日期");
    m_checkModel.setHeaderData(1,Qt::Horizontal,"第几节课");
    m_checkModel.setHeaderData(2,Qt::Horizontal,"课程名称");
    m_checkModel.setHeaderData(3,Qt::Horizontal,"学生姓名");
    m_checkModel.setHeaderData(4,Qt::Horizontal,"缺课类型");

    m_courseModel.setHeaderData(0,Qt::Horizontal,"课程名");
    m_courseModel.setHeaderData(1,Qt::Horizontal,"学分");
    m_courseModel.setHeaderData(2,Qt::Horizontal,"开课时间");

    m_teacherModel.setHeaderData(0,Qt::Horizontal,"工号");
    m_teacherModel.setHeaderData(1,Qt::Horizontal,"姓名");
    m_teacherModel.setHeaderData(2,Qt::Horizontal,"职称");

    //初始化学生数据
    QFile file;
    QString lineStr;
    QStringList strList;
    QList<QStandardItem*> itemList;
    QTextStream stream(&file);
    file.setFileName("student.txt");
    if(!file.open(QIODevice::ReadOnly)){
        QMessageBox::warning(0,"警告","学生信息初始化失败.");
        return;
    }
    for(;!stream.atEnd();){
        itemList.clear();

        lineStr = stream.readLine().simplified();
        strList = lineStr.split(' ');

        QStandardItem* item = new QStandardItem;
        item->setText(strList.at(0));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(1));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(2));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(3));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(4));
        itemList.append(item);

        m_studentModel.appendRow(itemList);
    }
    ui->tvStu->setModel(&m_studentModel);
    ui->tvStudentWIndow->setModel(&m_studentModel);
    file.close();

    //初始化考勤模型
    file.setFileName("check.txt");
    if(!file.open(QIODevice::ReadOnly)){
        QMessageBox::warning(0,"警告","考勤信息初始化失败.");
        return;
    }
    for(;!stream.atEnd();){
        itemList.clear();

        lineStr = stream.readLine().simplified();
        strList = lineStr.split(' ');

        QStandardItem* item = new QStandardItem;
        item->setText(strList.at(0));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(1));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(2));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(3));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(4));
        itemList.append(item);

        m_checkModel.appendRow(itemList);
    }
    ui->tvCheck->setModel(&m_checkModel);
    file.close();

//    //初始化课程信息
//    file.setFileName("course.txt");
//    if(!file.open(QIODevice::ReadOnly)){
//        QMessageBox::warning(0,"警告","课程信息初始化失败.");
//        return;
//    }
//    for(;!stream.atEnd();){
//        itemList.clear();

//        lineStr = stream.readLine().simplified();
//        strList = lineStr.split(' ');

//        QStandardItem* item = new QStandardItem;
//        item->setText(strList.at(0));
//        itemList.append(item);

//        item = new QStandardItem;
//        item->setText(strList.at(1));
//        itemList.append(item);

//        item = new QStandardItem;
//        item->setText(strList.at(2));
//        itemList.append(item);

//        m_courseModel.appendRow(itemList);
//    }
//    file.close();

//    //初始化教师信息
//    file.setFileName("teacher.txt");
//    if(!file.open(QIODevice::ReadOnly)){
//        QMessageBox::warning(0,"警告","教师信息初始化失败.");
//        return;
//    }
//    for(;!stream.atEnd();){
//        itemList.clear();

//        lineStr = stream.readLine().simplified();
//        strList = lineStr.split(' ');

//        QStandardItem* item = new QStandardItem;
//        item->setText(strList.at(0));
//        itemList.append(item);

//        item = new QStandardItem;
//        item->setText(strList.at(1));
//        itemList.append(item);

//        item = new QStandardItem;
//        item->setText(strList.at(2));
//        itemList.append(item);

//        m_teacherModel.appendRow(itemList);
//    }
    //    file.close();
}

void MainWindow::on_appendStu_accept(QString id, QString name, QString sex, QString age, QString course)
{
    for(int i = 0; i < m_studentModel.rowCount(); ++i){
        if(m_studentModel.data(m_studentModel.index(i,0)).toString() == id){
            QMessageBox::warning(0,"警告","添加失败,已有相同学号的学生.");
            return;
        }
    }

    QList<QStandardItem*> items;
    QStandardItem* item;

    item = new QStandardItem;
    item->setText(id);
    items.append(item);

    item = new QStandardItem;
    item->setText(name);
    items.append(item);

    item = new QStandardItem;
    item->setText(sex);
    items.append(item);

    item = new QStandardItem;
    item->setText(age);
    items.append(item);

    item = new QStandardItem;
    item->setText(course);
    items.append(item);

    m_studentModel.appendRow(items);
}

void MainWindow::on_appendCheck_accept(QString date,QString index,QString className,QString stuName,QString type)
{

    QList<QStandardItem*> items;
    QStandardItem* item;

    item = new QStandardItem;
    item->setText(date);
    items.append(item);

    item = new QStandardItem;
    item->setText(index);
    items.append(item);

    item = new QStandardItem;
    item->setText(className);
    items.append(item);

    item = new QStandardItem;
    item->setText(stuName);
    items.append(item);

    item = new QStandardItem;
    item->setText(type);
    items.append(item);

    m_checkModel.appendRow(items);
}


void MainWindow::on_actStudentManage_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagStu);
}


void MainWindow::on_avtCheckManage_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagCheck);
}


void MainWindow::on_btnAppendStu_clicked()
{
    appendStudent* a = new appendStudent;
    connect(a,SIGNAL(appendStu(QString,QString,QString,QString,QString)),this,SLOT(on_appendStu_accept(QString,QString,QString,QString,QString)));
    a->exec();
    a->deleteLater();
}


void MainWindow::on_btnDeleteStu_clicked()
{
    if(m_studentModel.rowCount() == 0){
        QMessageBox::warning(0,"警告","记录为空!");
        return;
    }

    if(ui->tvStu->currentIndex().isValid()){
        m_studentModel.removeRow(ui->tvStu->currentIndex().row());
    }

}


void MainWindow::on_btnResetStu_clicked()
{
    m_studentModel.removeRows(0,m_studentModel.rowCount());

    QFile file;
    QString lineStr;
    QStringList strList;
    QList<QStandardItem*> itemList;
    QTextStream stream(&file);
    file.setFileName("student.txt");
    if(!file.open(QIODevice::ReadOnly)){
        QMessageBox::warning(0,"警告","学生信息重置失败.");
        return;
    }
    for(;!stream.atEnd();){
        itemList.clear();

        lineStr = stream.readLine().simplified();
        strList = lineStr.split(' ');

        QStandardItem* item = new QStandardItem;
        item->setText(strList.at(0));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(1));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(2));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(3));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(4));
        itemList.append(item);

        m_studentModel.appendRow(itemList);
    }
    ui->tvStu->setModel(&m_studentModel);
    file.close();
}


void MainWindow::on_btnAppendCheck_clicked()
{

    appendCheck* a = new appendCheck;
    connect(a,SIGNAL(append(QString,QString,QString,QString,QString)),this,SLOT(on_appendCheck_accept(QString,QString,QString,QString,QString)));
    a->exec();
    a->deleteLater();
}


void MainWindow::on_btnDeleteCheck_clicked()
{
    if(m_checkModel.rowCount() == 0){
        QMessageBox::warning(0,"警告","记录为空!");
        return;
    }

    if(ui->tvCheck->currentIndex().isValid()){
        m_checkModel.removeRow(ui->tvCheck->currentIndex().row());
    }
}


void MainWindow::on_btnResetCheck_clicked()
{
    m_checkModel.removeRows(0,m_checkModel.rowCount());

    QFile file;
    QString lineStr;
    QStringList strList;
    QList<QStandardItem*> itemList;
    QTextStream stream(&file);
    file.setFileName("check.txt");
    if(!file.open(QIODevice::ReadOnly)){
        QMessageBox::warning(0,"警告","考勤信息重置失败.");
        return;
    }
    for(;!stream.atEnd();){
        itemList.clear();

        lineStr = stream.readLine().simplified();
        strList = lineStr.split(' ');

        QStandardItem* item = new QStandardItem;
        item->setText(strList.at(0));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(1));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(2));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(3));
        itemList.append(item);

        item = new QStandardItem;
        item->setText(strList.at(4));
        itemList.append(item);

        m_checkModel.appendRow(itemList);
    }
    ui->tvCheck->setModel(&m_checkModel);
    file.close();
}


void MainWindow::on_btnFind_clicked()
{
    if(ui->editFindStu->text().isEmpty()){//如果搜索内容为空就显示所有信息
        for(int i = 0;i < m_studentModel.rowCount(); ++i){
            ui->tvStu->showRow(i);
        }
    }
    else{
        if(ui->combFindTypeStu->currentIndex() == 0){//按学号查找
            for(int i = 0;i < m_studentModel.rowCount(); ++i){
                ui->tvStu->hideRow(i);
                if(m_studentModel.data(m_studentModel.index(i,0)).toString().contains(ui->editFindStu->text())){
                    ui->tvStu->showRow(i);
                }
            }
        }else{//按姓名查找
            for(int i = 0;i < m_studentModel.rowCount(); ++i){
                ui->tvStu->hideRow(i);
                if(m_studentModel.data(m_studentModel.index(i,1)).toString().contains(ui->editFindStu->text())){
                    ui->tvStu->showRow(i);
                }
            }
        }

        //计算查找到的结果数量
        int count = 0;
        for(int i = 0;i < m_studentModel.rowCount(); ++i){
            if(!ui->tvStu->isRowHidden(i)){
                ++count;
            }
        }
        if(count == 0){
            QMessageBox::information(0,"提示","未找到相关信息.");
        }
    }

}


void MainWindow::on_btnFindCheck_clicked()
{
    if(ui->editFindCheck->text().isEmpty()){//如果搜索内容为空就显示所有信息
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            ui->tvCheck->showRow(i);
        }
    }
    else{
        if(ui->combFindTypeCheck->currentIndex() == 0){//按课程名称统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,2)).toString().contains(ui->editFindCheck->text())){
                    ui->tvCheck->showRow(i);
                }
            }
        }else{//按学生姓名统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,3)).toString().contains(ui->editFindCheck->text())){
                    ui->tvCheck->showRow(i);
                }
            }
        }

        //计算查找到的结果数量
        int count = 0;
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            if(!ui->tvCheck->isRowHidden(i)){
                ++count;
            }
        }
        if(count == 0){
            QMessageBox::information(0,"提示","未找到相关信息.");
        }
    }

}


void MainWindow::on_btnStaCheck_clicked()
{
    if(ui->editFindCheck->text().isEmpty()){//如果搜索内容为空就显示所有信息
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            ui->tvCheck->showRow(i);
        }
    }
    else{
        if(ui->combFindTypeCheck->currentIndex() == 0){//按课程名称统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,2)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "旷课"){
                    ui->tvCheck->showRow(i);
                }
            }
        }else{//按学生姓名统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,3)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "旷课"){
                    ui->tvCheck->showRow(i);
                }
            }
        }
        ui->tvCheck->sortByColumn(0,Qt::SortOrder::AscendingOrder);

        //计算查找到的结果数量
        int count = 0;
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            if(!ui->tvCheck->isRowHidden(i)){
                ++count;
            }
        }
        if(count == 0){
            QMessageBox::information(0,"提示","未统计到相关信息.");
        }
    }
}


void MainWindow::on_actSave_triggered()
{
    //写入学生信息
    QFile file;
    QTextStream stream(&file);
    file.setFileName("student.txt");
    if(!file.open(QIODevice::WriteOnly)){
        QMessageBox::warning(0,"警告","学生信息保存失败.");
        return;
    }
    for(int i = 0; i < m_studentModel.rowCount(); ++i){
        QString str;

        str += m_studentModel.data(m_studentModel.index(i,0)).toString() +" ";
        str += m_studentModel.data(m_studentModel.index(i,1)).toString() +" ";
        str += m_studentModel.data(m_studentModel.index(i,2)).toString() +" ";
        str += m_studentModel.data(m_studentModel.index(i,3)).toString() +" ";
//        if(i == m_studentModel.columnCount() - 1){//如果是最后一行数据则不加换行符
//            str += m_studentModel.data(m_studentModel.index(i,4)).toString();
//        }else{//如果不是最后一行数据则添加换行符
            str += m_studentModel.data(m_studentModel.index(i,4)).toString() + "\n";
//        }

        stream << str;
    }
    file.close();

    //写入考勤信息
    file.setFileName("check.txt");
    if(!file.open(QIODevice::WriteOnly)){
        QMessageBox::warning(0,"警告","考勤信息保存失败.");
        return;
    }
    for(int i = 0; i < m_checkModel.rowCount(); ++i){
        QString str;

        str += m_checkModel.data(m_checkModel.index(i,0)).toString() +" ";
        str += m_checkModel.data(m_checkModel.index(i,1)).toString() +" ";
        str += m_checkModel.data(m_checkModel.index(i,2)).toString() +" ";
        str += m_checkModel.data(m_checkModel.index(i,3)).toString() +" ";
//        if(i == m_checkModel.columnCount() - 1){//如果是最后一行数据则不加换行符
//            str += m_checkModel.data(m_checkModel.index(i,4)).toString();
//        }else{//如果不是最后一行数据则添加换行符
            str += m_checkModel.data(m_checkModel.index(i,4)).toString() + "\n";
//        }

        stream << str;
    }
    file.close();

    QMessageBox::information(0,"提示","文件已保存在程序启动路径!");

}


void MainWindow::on_actStuWindow_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagSutWIndow);
}


void MainWindow::on_btnStaCheck_2_clicked()
{
    if(ui->editFindCheck->text().isEmpty()){//如果搜索内容为空就显示所有信息
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            ui->tvCheck->showRow(i);
        }
    }
    else{
        if(ui->combFindTypeCheck->currentIndex() == 0){//按课程名称统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,2)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "早退"){
                    ui->tvCheck->showRow(i);
                }
            }
        }else{//按学生姓名统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,3)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "早退"){
                    ui->tvCheck->showRow(i);
                }
            }
        }
        ui->tvCheck->sortByColumn(0,Qt::SortOrder::AscendingOrder);

        //计算查找到的结果数量
        int count = 0;
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            if(!ui->tvCheck->isRowHidden(i)){
                ++count;
            }
        }
        if(count == 0){
            QMessageBox::information(0,"提示","未统计到相关信息.");
        }
    }
}


void MainWindow::on_btnStaCheck_3_clicked()
{
    if(ui->editFindCheck->text().isEmpty()){//如果搜索内容为空就显示所有信息
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            ui->tvCheck->showRow(i);
        }
    }
    else{
        if(ui->combFindTypeCheck->currentIndex() == 0){//按课程名称统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,2)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "迟到"){
                    ui->tvCheck->showRow(i);
                }
            }
        }else{//按学生姓名统计
            for(int i = 0;i < m_checkModel.rowCount(); ++i){
                ui->tvCheck->hideRow(i);
                if(m_checkModel.data(m_checkModel.index(i,3)).toString().contains(ui->editFindCheck->text()) && m_checkModel.data(m_checkModel.index(i,4)).toString() == "迟到"){
                    ui->tvCheck->showRow(i);
                }
            }
        }
        ui->tvCheck->sortByColumn(0,Qt::SortOrder::AscendingOrder);

        //计算查找到的结果数量
        int count = 0;
        for(int i = 0;i < m_checkModel.rowCount(); ++i){
            if(!ui->tvCheck->isRowHidden(i)){
                ++count;
            }
        }
        if(count == 0){
            QMessageBox::information(0,"提示","未统计到相关信息.");
        }
    }
}

